package dao

import (
	"database/sql"
	"goWeb/bookStore/model"
	"goWeb/bookStore/utils"
	"strconv"
)

func GetAllBooks() (books []*model.Book, err error) {
	sqlstr := "select id,title,author,price,sales,stock,img_path from bs_book"
	rows, err := utils.Db.Query(sqlstr)
	//books = make([]*model.Book, 1)
	for rows.Next() {
		book := &model.Book{}
		rows.Scan(&book.Id, &book.Title, &book.Author, &book.Price, &book.Sales, &book.Stock, &book.Img_path)
		//fmt.Println("在getAllBooks中查看当前book：", book)
		books = append(books, book)
	}
	return books, err
}

func AddBook(b *model.Book) (err error) {
	sqlstr := "insert into bs_book(title,author,price,sales,stock,img_path) values(?,?,?,?,?,?)"
	_, err = utils.Db.Exec(sqlstr, b.Title, b.Author, b.Price, b.Sales, b.Stock, b.Img_path)
	return err
}

func DelBook(id int) (err error) {
	sqlstr := "delete from bs_book where Id=?"
	_, err = utils.Db.Exec(sqlstr, id)
	return err
}

func GetBookById(id int) (book *model.Book) {
	sqlstr := "select id,title,author,price,sales,stock from bs_book where id=? "
	row := utils.Db.QueryRow(sqlstr, id)
	book = &model.Book{}
	row.Scan(&book.Id, &book.Title, &book.Author, &book.Price, &book.Sales, &book.Stock)
	return book
}

func UpdateBook(b *model.Book) (err error) {
	sqlstr := "update  bs_book set title=? ,author=?,price=?,sales=?,stock=? where id=?"
	_, err = utils.Db.Exec(sqlstr, b.Title, b.Author, b.Price, b.Sales, b.Stock, b.Id)
	return err
}

//获取带页数的图书信息
func GetPageBooks(pageNo string, minPrice string, maxPrice string) (page *model.Page, err error) {
	ipageNo, _ := strconv.ParseInt(pageNo, 10, 0)
	page = &model.Page{}
	var sqlstr string
	var row *sql.Row
	if minPrice == "" && maxPrice == "" {
		sqlstr = "select count(1) from bs_book"
		row = utils.Db.QueryRow(sqlstr)
	} else {
		sqlstr = "select count(1) from bs_book where price between ? and ?"
		row = utils.Db.QueryRow(sqlstr, minPrice, maxPrice)
	}

	//设置当前页数
	page.PageNo = int(ipageNo)
	//设置总记录数
	row.Scan(&page.RecordCount)
	//设置每页条数
	page.PageSize = 4
	//计算总页数
	if page.RecordCount%page.PageSize != 0 {
		page.PageCount = page.RecordCount/page.PageSize + 1
	} else {
		page.PageCount = page.RecordCount / page.PageSize
	}
	//得到当前页数的图书信息
	var sqlstr2 string
	var rows *sql.Rows
	if minPrice == "" && maxPrice == "" {
		sqlstr2 = "select id,title,author,price,sales,stock,img_path from bs_book limit ?,?"
		rows, err = utils.Db.Query(sqlstr2, (int(ipageNo)-1)*page.PageSize, page.PageSize)
	} else {
		sqlstr2 = "select id,title,author,price,sales,stock,img_path from bs_book where price between ? and ? limit ?,?"
		rows, err = utils.Db.Query(sqlstr2, minPrice, maxPrice, (int(ipageNo)-1)*page.PageSize, page.PageSize)
	}

	//books = make([]*model.Book, 1)
	var books []*model.Book
	for rows.Next() {
		book := &model.Book{}
		rows.Scan(&book.Id, &book.Title, &book.Author, &book.Price, &book.Sales, &book.Stock, &book.Img_path)
		//fmt.Println("在getAllBooks中查看当前book：", book)
		books = append(books, book)
	}
	page.Books = books
	return page, err
}
